package com.igeek.ch01;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;

@WebServlet(name = "SecondServlet" , urlPatterns = "/second" , loadOnStartup = 1)
public class SecondServlet extends HttpServlet {

    public SecondServlet(){
        System.out.println("SecondServlet() 实例化");
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("SecondServlet doPost()");

        //1.处理请求的编码集
        request.setCharacterEncoding("UTF-8");

        //2.接收请求参数  表单方式，以input的name的值作为key，以输入框中填写数据作为value
        String name = request.getParameter("name");
        String pwd = request.getParameter("pwd");
        String[] hobbies = request.getParameterValues("hobby");
        System.out.println("name = "+name+" , pwd = "+pwd+" , hobbies = "+ Arrays.toString(hobbies));

        //3.响应方式
        //设置响应的内容为HTML代码，及编码集
        /*response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        if(name!=null && !name.equals("") && pwd!=null && !pwd.equals("")) {
            out.println("<h1 style='color:green'>成功</h1>");
        }else{
            out.println("<h1 style='color:red'>失败</h1>");
        }*/

        //设置响应的内容为JSON数据，及编码集  {"msg":"成功"}
        response.setContentType("application/json;charset=UTF-8");
        PrintWriter out = response.getWriter();
        if(name!=null && !name.equals("") && pwd!=null && !pwd.equals("")) {
            out.println("{\"msg\":\"成功\"}");
        }else{
            out.println("{\"msg\":\"失败\"}");
        }
        //刷新流，及关闭流资源
        out.flush();
        out.close();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("SecondServlet doGet()");

        //1.处理请求的编码集
        request.setCharacterEncoding("UTF-8");

        //2.接收请求参数  url?key1=value1&key2=value2&...
        //接收单个数据
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        //接收多个数据
        String[] hobbies = request.getParameterValues("hobby");
        System.out.println("username = "+username+" , password = "+password+" , hobbies = "+ Arrays.toString(hobbies));

        //3.响应方式  跳转页面
        /*
            请求转发 和 响应重定向 的区别
            1.请求转发 地址栏显示的请求地址
            响应重定向 地址栏显示的目标地址
            2.请求转发 携带请求数据
            响应重定向 会产生新的请求，不能携带数据
            3.请求转发 查询
            响应重定向 增删改
         */
        if(username!=null && !username.equals("") && password!=null && !password.equals("")){
            //请求转发
            request.getRequestDispatcher("success.html").forward(request,response);
        }else{
            //响应重定向
            response.sendRedirect("fail.html");
        }
    }
}
